<?php
/**
 *  PHP Template
 *
 * @author: ZDW
 * @date: 2016-08-08
 * @version: $Id$
 */

namespace App\Repositories;

use App\Models\Cimo\CompanyInvite;
use App\Models\Cimo\UserComment;
use App\Models\Cimo\UserResumeTag;

class CompanyInviteRepository implements BaseRepositoryInterface
{
    use BaseRepository;

    private $model;

    public function __construct(CompanyInvite $model)
    {
        $this->model = $model;
    }

    /**
     * store.
     *
     * @param array $input
     */
    public function store($input)
    {
        return $this->savePost($this->model, $input);
    }

    /**
     * save.
     *
     * @param Request $input 输入
     */
    public function savePost($events, $input)
    {
        $events->fill($input);
        $events->save();

        return $events;

    }
    
    public function getByNeed($needId)
    {
        return $this->model->where('need_id',$needId)->get();
    }

    /**
     * 更新
     * @param int $id
     * @param $input
     * @return mixed
     */
    public function update($id, $input)
    {
        $model = $this->model->find($id);

        return $this->savePost($model, $input);
    }
    
    public function getLists($userId = 0, $companyId = 0,$status = 0,$pageSize = 20)
    {
        $query = $this->model->with('need','user','company','comment');
        
        if($userId) {
            $query->where('user_id',$userId);
        }
        
        if($companyId) {
            $query->where('company_id',$companyId);
        }
        
        if($status || $status == 0)
        {
            if(is_array($status)) {
                $query->whereIn('status',$status);                
            } else {
                $query->where('status',$status);
            }
        }
        
        return $query->orderBy('created_at','desc')->paginate($pageSize);
        
            
    }

    /**
     * 数据列表
     * @param array $where 筛选条件
     * @param array $fields 追加查询字段
     * @param int $pagesize
     * @return mixed
     */
    public function ls(array $where, array $fields = NULL, $pagesize = 15)
    {
        $query = CompanyInvite::select('user_resumes.name', 'user_resumes.highest_degree', 'user_resumes.gznx',
            'user_resumes.phone', 'user_resumes.email', 'user_resumes.signature', 'user_resumes.intro',
            'company_invite.*', 'company.name AS company', 'users.name AS username', 'users.real_name')
            ->join('users', 'users.id', '=', 'company_invite.user_id')
            ->join('user_resumes', 'user_resumes.id', '=', 'company_invite.resume_id')
            ->join('company', 'company.id', '=', 'company_invite.company_id');
        $query = self::_where($where, $query);
        if ($fields)
            foreach ($fields as $field) {
                $query->addSelect($field);
            }
        $result = $query->orderBy('company_invite.created_at', 'DESC')->paginate($pagesize);
        if ($result)
            foreach ($result as &$item) {
                $item->tags = UserResumeTag::select('tags.tags_name')
                    ->join('tags', 'tags.tags_id', '=', 'user_resume_tag.tag_id')
                    ->where('user_resume_tag.resume_id', '=', $item->resume_id)->get();
                $item->comment = UserComment::where('invite_id',$item->invite_id)->first();
            }
        return $result;
    }

    /**
     * 筛选条件
     * @param array $where
     * @param mixed $query
     * @return mixed
     */
    private static function _where(array $where, &$query)
    {
        if (!$where) return $query;
        foreach ($where as $v) {
            list($field, $op, $val) = $v;
            if ($op === 'between'):
                $query->whereBetween($field, $val);
            elseif ($op === 'in'):
                $query->whereIn($field, $val);
            else:
                $query->where($field, $op, $val);
            endif;
        }
        return $query;
    }

    public function selectAll()
    {
        return CompanyInvite::all();
    }

    public function find($id)
    {
        return CompanyInvite::find($id);
    }

    public function getByIds($ids)
    {
        return $this->model->whereIn('invite_id',$ids)->get();
    }
}